package com.demo.bookstore.servlet;

import com.demo.bookstore.bean.BookOrder;
import com.demo.bookstore.bean.Cart;
import com.demo.bookstore.bean.User;
import com.demo.bookstore.service.OrderService;
import com.demo.bookstore.service.impl.OderServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ddone
 * @date 2024/3/16-23:42
 */
@WebServlet("/order")
public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OderServiceImpl();
    private ObjectMapper MAPPER = new ObjectMapper();
    /**
     * @param req
     * @param resp
     * @throws IOException
     */
    public void addOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        User user = (User) req.getSession().getAttribute("book_user");
        String orderNo = orderService.addOrder(cart,user);
        req.setAttribute( "orderNo",orderNo);
        req.getSession().removeAttribute("cart");
        this.processTemplate("cart/checkout",req,resp);
    }

    public void findOrderByUserId(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        User user = (User) req.getSession().getAttribute("book_user");
        List<BookOrder> orderList= orderService.findOrderByUserId(user.getUserid());
        req.setAttribute("orderList",orderList);
        this.processTemplate("order/order", req, resp);
    }
}
